% 1. id = subject id
% 2. trial = trial number (note some trials are missing as there were no gaze dwells on the symbols)
% 3. fixtime = gaze dwell duration on the symbol
% 4. first = 1 if this is the first dwell within the trial
% 5. middle = 1 if it�s not the first and not the last
% 6. last = 1 if it�s the last one
% 7. nfix = total number of gaze dwells on this trials
% 8. fixab = fixation was on symbol A or B (I don�t remember exactly if 1 means A or B, but it�s easy to figure out - more dwells are always on the chosen symbol).
% 

clc;
clearvars;

GazeDataFileName='Data\HumanData_KonovalovGazeData.mat';


ObservData=load('Data\HumanData_KonovalovDataPart1.mat');

[Data,Head]=xlsread('../FromKonovalov/learning_gazedata.xlsx');
Data(:,1)=[];%% Delete Index that dose not needed!
SubjNum=45;
MeanDwell=NaN(SubjNum,4);% First Midel Last All
NFix=NaN(SubjNum,150);
ChosenMeanDwell=NaN(SubjNum,150);
NotChosenMeanDwell=NaN(SubjNum,150);
MeanNFix=NaN(SubjNum,1);
MeanChosenMeanDwell=NaN(SubjNum,1);
MeanNotChosenMeanDwell=NaN(SubjNum,1);
HistNFix=NaN(SubjNum,6);
PLastSeenChosen=NaN(SubjNum,2); %More gaze time for last symbol 
                                %More gaze time for other symbol
LastSeenSymbol=NaN(SubjNum,150);
LastSeenDwell=NaN(SubjNum,150);
ChoseLastSeen=NaN(SubjNum,150);
TotalGazeOnAandB=NaN(1,2);
MoreGazeTimeForLastSymbol=NaN(SubjNum,150);
MoreGazeTimeForNotLastSymbol=NaN(SubjNum,150);
for s=1:SubjNum
    MeanDwell(s,1)=mean(Data((Data(:,1)==s & Data(:,4)==1),3));
    MeanDwell(s,2)=mean(Data((Data(:,1)==s & Data(:,5)==1),3));
    MeanDwell(s,3)=mean(Data((Data(:,1)==s & Data(:,6)==1),3));
    MeanDwell(s,4)=mean(Data((Data(:,1)==s               ),3));
    for t=1:150
        if any((Data(:,1)==s & Data(:,2)==t))
    NFix(s,t)=mean(Data((Data(:,1)==s & Data(:,2)==t),7));
    ChosenMeanDwell(s,t)=mean(Data((Data(:,1)==s & Data(:,2)==t) & Data(:,8)==(ObservData.ASARCMat(t,1,s)-1),3));
    NotChosenMeanDwell(s,t)=mean(Data((Data(:,1)==s & Data(:,2)==t) & Data(:,8)~=(ObservData.ASARCMat(t,1,s)-1),3));
    LastSeenSymbol(s,t)=mean(Data((Data(:,1)==s & Data(:,2)==t & Data(:,6)==1) , 8))+1;
    LastSeenDwell(s,t)= mean(Data((Data(:,1)==s & Data(:,2)==t & Data(:,6)==1) , 3));
    ChoseLastSeen(s,t)=ObservData.ASARCMat(t,1,s)==LastSeenSymbol(s,t);
    
    TotalGazeOnAandB(1,1)=sum(Data((Data(:,1)==s & Data(:,2)==t & Data(:,8)==0 ),3));
    TotalGazeOnAandB(1,2)=sum(Data((Data(:,1)==s & Data(:,2)==t & Data(:,8)==1 ),3));
    MoreGazeTimeForLastSymbol(s,t)=TotalGazeOnAandB(LastSeenSymbol(s,t))>TotalGazeOnAandB(3-LastSeenSymbol(s,t));
        end
    end
    MoreGazeTimeForNotLastSymbol(s,:)=MoreGazeTimeForLastSymbol(s,:);
    MoreGazeTimeForNotLastSymbol(s,MoreGazeTimeForNotLastSymbol(s,:)==1)=2;
    MoreGazeTimeForNotLastSymbol(s,MoreGazeTimeForNotLastSymbol(s,:)==0)=1;
    MoreGazeTimeForNotLastSymbol(s,MoreGazeTimeForNotLastSymbol(s,:)==2)=0;
    MeanNFix(s)=mean(NFix(s,:),'omitnan');
    MeanChosenMeanDwell(s)=mean(ChosenMeanDwell(s,:),'omitnan');
    MeanNotChosenMeanDwell(s)=mean(NotChosenMeanDwell(s,:),'omitnan');
    HistNFix(s,:)=hist(NFix(s,:),1:6);
    I=find(~isnan(MoreGazeTimeForLastSymbol(s,:)));
    PLastSeenChosen(s,1)=sum(ChoseLastSeen(s,I).*MoreGazeTimeForLastSymbol(s,I)   )/sum(MoreGazeTimeForLastSymbol(s,I));
    PLastSeenChosen(s,2)=sum(ChoseLastSeen(s,I).*MoreGazeTimeForNotLastSymbol(s,I))/sum(MoreGazeTimeForNotLastSymbol(s,I));
end
save(GazeDataFileName,'MeanDwell','NFix','MeanNFix','HistNFix','MeanChosenMeanDwell','MeanNotChosenMeanDwell','ChosenMeanDwell','NotChosenMeanDwell','PLastSeenChosen');